/**  
* @author hongchun.li  
* @date 2013-4-21 下午02:28:48
* @version V1.0  
*/ 
package com.apis.util.dao;
import java.io.Serializable;  
import java.util.List;  
import org.hibernate.Query;  
import org.hibernate.Session;  
import org.hibernate.Transaction;

import com.apis.util.HibernateUtil;
import com.apis.util.PageUtil;
  
/**
 * @author hongchun.li
 *
 */
public class BaseDaoImpl  {
	 
    /**
     * 添加
     * 
     * @param obj
     */
    public void save(Object obj) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            session.save(obj);
            tx.commit();
 
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
 
    /**
     * 删除
     * 
     * @param obj
     */
    public void delete(Object obj) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            session.delete(obj);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
 
    /**
     * 根据主键删除
     * 
     * @param clazz
     * @param id
     */
    public void deleteById(Class clazz, Serializable id) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            session.delete(session.get(clazz, id));
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
 
    /**
     * 修改
     * 
     * @param obj
     */
    public void update(Object obj) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            session.update(obj);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
 
    }
 
    /**
     * 根据主键查询
     * 
     * @param clazz
     * @param id
     * @return
     */
    public Object get(Class clazz, Serializable id) {
        Session session = null;
 
        try {
            session = HibernateUtil.getSession();
            Object obj = session.get(clazz, id);
            return obj;
        } finally {
            if (session != null) {
                session.close();
            }
        }
 
    }
 
    /**
     * 根据多个属性查询
     * 
     * @param clazz
     * @param strs
     * @return
     */
    public Object getByNProperty(Class clazz, String... strs) {
 
        if (strs != null && strs.length != 0 && 0 != strs.length % 2) {
            StringBuffer hql = new StringBuffer("select model from "
                    + clazz.getName() + " as model where ");
            for (int i = 0; i < strs.length; i += 2) {
                hql.append(" " + strs[i] + " = " + strs[i + 1]);
            }
 
            Session session = null;
            try {
                session = HibernateUtil.getSession();
                List<Object> objs = session.createQuery(hql.toString()).list();
                if (objs != null && objs.size() != 0) {
                    return objs.get(0);
                } else {
                    return null;
                }
            } finally {
                if (session != null) {
                    session.close();
                }
            }
        } else {
            return null;
        }
 
    }
 
    /**
     * 根据属性查询
     * 
     * @param clazz
     * @param pName
     * @param pValue
     * @return
     */
    public Object getUniqueByProperty(Class clazz, String pName, Object pValue) {
        Session session = null;
 
        try {
            session = HibernateUtil.getSession();
            String hql = "select model from " + clazz.getName()
                    + " as model where model." + pName + " = '" + pValue + "'";
            List<Object> objs = session.createQuery(hql).list();
            if (objs != null && objs.size() != 0) {
                return objs.get(0);
            } else {
                return null;
            }
 
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
 
    /**
     * 根据HQL查询
     * 
     * @param hql
     * @return
     */
    public Object getUniqueByHql(String hql) {
 
        Session session = null;
 
        try {
            session = HibernateUtil.getSession();
            List<Object> objs = session.createQuery(hql).list();
            if (objs != null && objs.size() != 0) {
                return objs.get(0);
            } else {
                return null;
            }
 
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
 
    /**
     * 根据SQL查询
     * 
     * @param sql
     * @param clazz
     * @return
     */
    public Object getUniqueBySql(String sql, Class clazz) {
 
        Session session = null;
 
        try {
            session = HibernateUtil.getSession();
            Query query = session.createSQLQuery(sql).addEntity(clazz);
            List<Object> objs = query.list();
            if (objs != null && objs.size() != 0) {
                return objs.get(0);
            } else {
                return null;
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
 
    // ////////////////////查询单个完毕////////////////
 
    /**
     * 查询所有
     */
    public List<Object> getList(Class clazz) {
        Session session = null;
 
        try {
            session = HibernateUtil.getSession();
            String hql = "select model from " + clazz.getName() + " as model ";
            List list = session.createQuery(hql).list();
            return list;
        } finally {
            if (session != null) {
                session.close();
            }
        }
 
    }
 
    /**
     * 根据属性查询 全部
     * 
     * @param clazz
     * @param pName
     * @param pValue
     * @return
     */
    public List<Object> getListByProperty(Class clazz, String pName,
            Object pValue) {
        Session session = null;
 
        try {
            session = HibernateUtil.getSession(); // 获得安全session
            String hql = "select model from " + clazz.getName()
                    + " as model where model." + pName + " = '" + pValue + "'";
            return session.createQuery(hql).list();
 
        } finally {
            if (session != null) {
                session.close();
            }
        }
 
    }
 
    /**
     * 根据属性和条件查询 全部
     * 
     * @param clazz
     * @param pName
     * @param pValue
     * @param condition
     * @return
     */
    public List<Object> getListByProperty(Class clazz, String pName,
            Object pValue, String condition) {
        Session session = null;
 
        try {
            session = HibernateUtil.getSession();
            String hql = "select model from " + clazz.getName()
                    + " as model where model." + pName + " " + condition
                    + " '%" + pValue + "%'";
            List<Object> list = session.createQuery(hql).list();
            return list;
        } finally {
            if (session != null) {
                session.close();
            }
        }
 
    }
 
    /**
     * 根据多个属性模糊查询
     * 
     * @param clazz
     * @param strs
     * @return
     */
    public List getListByNProperty(Class clazz, String... strs) {
 
        if (strs != null && strs.length != 0 && 0 != strs.length % 2) {
            StringBuffer hql = new StringBuffer("select model from "
                    + clazz.getName() + " as model where ");
            for (int i = 0; i < strs.length; i += 2) {
                hql.append(" " + strs[i] + " Like  %" + strs[i + 1] + "% ");
            }
 
            Session session = null;
            try {
                session = HibernateUtil.getSession();
                List<Object> objs = session.createQuery(hql.toString()).list();
                return objs;
            } finally {
                if (session != null) {
                    session.close();
                }
            }
        } else {
            return null;
        }
 
    }
 
    /**
     * 根据SQL查询 全部
     * 
     * @param hql
     * @return
     */
    public List<Object> getListByHql(String hql) {
 
        Session session = null;
 
        try {
            session = HibernateUtil.getSession();
            List list = session.createQuery(hql).list();
            return list;
 
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
 
    /**
     * 根据SQL查询全部
     * 
     * @param sql
     * @param clazz
     * @return
     */
    public List getListBySql(String sql, Class clazz) {
 
        Session session = null;
 
        try {
            session = HibernateUtil.getSession();
            Query query = session.createSQLQuery(sql).addEntity(clazz);
            return query.list();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
 
    /**
     * 分页查询全部
     * 
     * @param calzz
     * @param pageUtil
     */
    public void getListByPage(Class calzz, PageUtil pageUtil) {
        String hql = "SELECT model from " + calzz.getName() + " as model";
        getListByPage(hql, pageUtil);
    }
 
    /**
     * 根据SQL分页查询
     * 
     * @param hql
     * @param pageUtil
     */
    public void getListByPage(String hql, PageUtil pageUtil) {
        if (null == hql) {
            return;
        }
        Session session = null;
 
        try {
            session = HibernateUtil.getSession();
            Query query = session.createQuery(hql);
            query.setFirstResult(pageUtil.getFirstRec());
            query.setMaxResults(pageUtil.getPageSize());
            pageUtil.setList(query.list());
 
            String queryString = "";
            if (hql.toUpperCase().indexOf("SELECT") != -1) {
                int i = query.getQueryString().toUpperCase().indexOf("FROM");
                queryString = "Select count(*) "
                        + hql.substring(i, hql.length());
            } else {
                queryString = "Select count(*) " + hql;
            }
            // 去掉ORDER BY 的部分
            int j = queryString.toUpperCase().lastIndexOf("ORDER");
            if (j != -1) {
                queryString = queryString.substring(0, j);
            }
            Query cquery = session.createQuery(queryString);
            cquery.setCacheable(true);
            int recTotal = ((Integer) cquery.iterate().next()).intValue();
            pageUtil.setRecTotal(recTotal);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}